package com.ss.android.article.base.feature.detail2.a.c;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.detail.model.MixedAdsData;
import com.ss.android.article.base.feature.detail.model.PhoneAdsData;
import com.ss.android.article.base.feature.detail2.widget.ProgressTextView;
import com.ss.android.article.base.feature.detail2.widget.ProgressTextView.Status;
import com.ss.android.article.base.feature.detail2.widget.a.BaseDetailLayout;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.video.IMediaViewLayout;
import com.ss.android.article.base.feature.video.IVideoController;
import com.ss.android.article.base.ui.DrawableButton;
import com.ss.android.article.base.ui.EllipsisTextView;
import com.ss.android.article.base.utils.ArticleHelper;
import com.ss.android.article.browser.R;
import com.ss.android.article.common.NightModeAsyncImageView;
import com.ss.android.article.common.e.VideoControllerSupplierProxy;
import com.ss.android.b.a.AdsAppItem;
import com.ss.android.b.a.BaseAdsData;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.image.Image;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.EnumSet;

public final class DetailAdVideoLayout extends BaseDetailLayout implements z {
    public com.ss.android.article.base.feature.detail2.a.b.a c;
    int a;
    AppData b;
    long d;
    String e;
    Article f;
    ProgressTextView g;
    JSONObject h;
    private IVideoController mVideoController;
    private View k;
    private NightModeAsyncImageView l;
    private EllipsisTextView m;
    private DrawableButton n;
    private TextView o;
    private View p;
    private TextView q;
    private int r;
    private int s;
    private String t;
    private com.ss.android.article.base.feature.detail2.a.aa u;
    private String v;
    private com.ss.android.article.base.feature.f.aa w;

    public DetailAdVideoLayout(Context arg2) {
        super(arg2);
        this.a = 1;
    }

    private void a(int arg3, int arg4) {
        ViewGroup.LayoutParams v0 = this.l.getLayoutParams();
        if (v0 != null) {
            if (arg3 > 0) {
                v0.width = arg3;
            }

            if (arg4 > 0) {
                v0.height = arg4;
            }

            this.l.setLayoutParams(v0);
        }
    }

    public final void initView() {
        super.initView();
        this.k = this.findViewById(com.ss.android.article.browser.R.id.ad_video_cover);
        this.l = this.findViewById(com.ss.android.article.browser.R.id.ad_cover_image);
        this.m = this.findViewById(com.ss.android.article.browser.R.id.ad_cover_title);
        this.findViewById(com.ss.android.article.browser.R.id.ad_cover_play_icon);
        this.n = this.findViewById(com.ss.android.article.browser.R.id.ad_cover_duration);
        this.o = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_label);
        this.p = this.findViewById(com.ss.android.article.browser.R.id.ad_download_area);
        this.q = this.findViewById(com.ss.android.article.browser.R.id.ad_source_tv_name);
        this.g = this.findViewById(com.ss.android.article.browser.R.id.ad_tv_creative);
        this.setBackgroundResource(com.ss.android.article.browser.R.drawable.detail_ad_bg);
        this.b = AppData.getInstance();
        if (this.b.getAbSettings().videoRecForDetailAd()) {
            this.w = new com.ss.android.article.base.feature.f.aa(this.getContext());
        } else if (VideoControllerSupplierProxy.getInstance().getVideoControllerInstance().isPauseFromList()) {
            this.mVideoController = VideoControllerSupplierProxy.getInstance().getVideoControllerInstance();
            VideoControllerSupplierProxy.getInstance().getVideoControllerInstance().reset(this.getContext(), ((ViewGroup) this), false, null);
        } else {
            this.mVideoController = VideoControllerSupplierProxy.getInstance().createVideoController(this.getContext(),
                    ((ViewGroup) this), false, EnumSet.of(IMediaViewLayout.CtrlFlag.hideCloseBtn, IMediaViewLayout.CtrlFlag.fixedSize));
        }
    }

    public final void setAdData(final BaseAdsData arg8) {
        String v1;
        BaseAdsData v0_1;
        int v5 = View.GONE;
        if (arg8 != null) {
            this.d = arg8.id;
            try {
                this.h = new JSONObject();
                this.h.put("log_extra", arg8.logExtra);
            } catch (JSONException v0) {
                v0.printStackTrace();
            }

            this.v = arg8.logExtra;
            this.e = arg8.pkg;
            this.u = new com.ss.android.article.base.feature.detail2.a.aa(arg8);
            if ((arg8 instanceof DownloadAppAd)) {
                v0_1 = arg8;
                this.p.setVisibility(View.VISIBLE);
                this.c = new com.ss.android.article.base.feature.detail2.a.b.a(this.getContext(), ((DownloadAppAd) v0_1));
                ProgressTextView v2 = this.g;
                v1 = android.support.design.a.isTextEmpty(((DownloadAppAd) v0_1).btnTxt) ? this.getResources().getString(com.ss.android.article.browser.R.string.j2) : ((DownloadAppAd) v0_1).btnTxt;
                v2.setText(((CharSequence) v1));
                this.g.setOnClickListener(new v(this));
                this.q.setText(((DownloadAppAd) v0_1).appName);
                if (!android.support.design.a.isTextEmpty(((DownloadAppAd) v0_1).appAdSourceLabel)) {
                    this.o.setText(((DownloadAppAd) v0_1).appAdSourceLabel);
                }

                this.s = this.b(((DownloadAppAd) v0_1).width, ((DownloadAppAd) v0_1).height);
                this.a(this.r, this.s);
                this.l.setUrl(((DownloadAppAd) v0_1).coverUrl);
                this.m.setText(((DownloadAppAd) v0_1).title);
                this.t = ((DownloadAppAd) v0_1).videoId;
                v1 = ArticleHelper.convert2ReadableString(((DownloadAppAd) v0_1).videoDuration);
                if (((DownloadAppAd) v0_1).videoDuration == 0) {
                    this.n.setVisibility(v5);
                } else {
                    this.n.setVisibility(View.VISIBLE);
                    this.n.setText(v1, true);
                }
            } else {
                if ((arg8 instanceof MixedAdsData)) {
                    v0_1 = arg8;
                    this.p.setVisibility(v5);
                    if (!android.support.design.a.isTextEmpty(((MixedAdsData) v0_1).h)) {
                        this.o.setText(((MixedAdsData) v0_1).h);
                    }

                    this.s = this.b(((MixedAdsData) v0_1).k, ((MixedAdsData) v0_1).l);
                    this.a(this.r, this.s);
                    this.l.setUrl(((MixedAdsData) v0_1).o);
                    this.m.setText(((MixedAdsData) v0_1).j);
                    this.t = ((MixedAdsData) v0_1).n;
                    v1 = ArticleHelper.convert2ReadableString(((MixedAdsData) v0_1).m);
                    if (((MixedAdsData) v0_1).m == 0) {
                        this.n.setVisibility(v5);
                    } else {
                        this.n.setVisibility(View.VISIBLE);
                        this.n.setText(v1, true);
                    }
                } else if (!(arg8 instanceof PhoneAdsData)) {
                } else {
                    v0_1 = arg8;
                    this.p.setVisibility(View.VISIBLE);
                    if (!android.support.design.a.isTextEmpty(((PhoneAdsData) v0_1).h)) {
                        this.o.setText(((PhoneAdsData) v0_1).h);
                    }

                    this.s = this.b(((PhoneAdsData) v0_1).videoWidth, ((PhoneAdsData) v0_1).videoHeight);
                    this.a(this.r, this.s);
                    this.l.setUrl(((PhoneAdsData) v0_1).videoCoverUrl);
                    this.m.setText(((PhoneAdsData) v0_1).j);
                    this.t = ((PhoneAdsData) v0_1).videoId;
                    this.q.setText(((PhoneAdsData) v0_1).sourceName);
                    v1 = ArticleHelper.convert2ReadableString(((PhoneAdsData) v0_1).videoDuration);
                    if (((PhoneAdsData) v0_1).videoDuration == 0) {
                        this.n.setVisibility(v5);
                    } else {
                        this.n.setVisibility(View.VISIBLE);
                        this.n.setText(v1, true);
                    }

                    if (!TextUtils.isEmpty(((PhoneAdsData) v0_1).phoneNumber) && !TextUtils.isEmpty(((PhoneAdsData) v0_1).buttonText)) {
                        this.g.setText(((PhoneAdsData) v0_1).buttonText);
                        this.g.setOnClickListener(new w(this, ((PhoneAdsData) v0_1)));
                    } else {
                        this.g.setVisibility(v5);
                    }
                }
            }

            View.OnClickListener v0_2 = new View.OnClickListener() {

                public final void onClick(View arg9) {
                    String v2 = (arg8 instanceof PhoneAdsData) ? "detail_call" : "detail_ad";
                    if(DetailAdVideoLayout.this.c != null) {
                        DetailAdVideoLayout.this.c.b();
                    }
                    else {
                        AdsAppItem.a(DetailAdVideoLayout.this.getContext(), arg8.openUrl, arg8.webUrl,
                                arg8.webTitle, arg8.orientation,
                                new AdsAppItem.b(DetailAdVideoLayout.this.getContext(), v2,
                                        "click", arg8.id, arg8.logExtra, 1));
                    }
                }
            };
            this.setOnClickListener(((View.OnClickListener) v0_2));
            this.m.setOnClickListener(((View.OnClickListener) v0_2));
            this.k.setOnClickListener(new u(this, arg8));
        }
    }

    public final void a(DownloadShortInfo arg5) {
        if (arg5 == null) {
            this.g.setStatus(Status.IDLE);
        } else {
            float v0 = (((float) arg5.d)) / (((float) arg5.c));
            this.a = arg5.b;
            switch (this.a) {
                case 1:
                case 2: {
                    label_15:
                    this.a = 2;
                    this.g.setStatus(Status.DOWNLOADING);
                    this.g.setProgress(v0);
                    return;
                }
                case 4: {
                    this.g.setStatus(Status.PAUSING);
                    this.g.setProgress(v0);
                    return;
                }
                case 8: {
                    if (ToolUtils.isAppInstalled(this.getContext(), this.e)) {
                        this.a = 32;
                        this.g.setStatus(Status.FINISH_OPEN);
                        this.u.a("detail_download_ad");
                        return;
                    } else {
                        this.g.setStatus(Status.FINISH_INSTALL);
                        return;
                    }
                }
                case 16: {
                    this.g.setStatus(Status.FAILURE);
                    return;
                }
            }

            return;
        }
    }

    public final void a(DownloadShortInfo arg10, int arg11, long arg12, long arg14) {
        this.post(new s(this, arg14, arg12, arg11, arg10));
    }

    public final void adaptNightMode(boolean arg4) {
        Resources v0 = this.getResources();
        this.q.setTextColor(v0.getColorStateList(com.ss.android.article.browser.R.color.ssxinzi1));
        this.m.setTextColor(v0.getColorStateList(com.ss.android.article.browser.R.color.ssxinzi8));
        this.g.b();
        int v0_1 = this.i == 0 ? R.drawable.aq : R.drawable.video_detail_download_area_bg;
        this.p.setBackgroundResource(v0_1);
        this.setBackgroundResource(com.ss.android.article.browser.R.drawable.detail_ad_bg);
        this.l.adaptNightMode(arg4);
    }

    private int b(int arg3, int arg4) {
        int v0;
        if (arg3 == 0 || arg4 == 0) {
            v0 = 0;
        } else {
            this.r = this.getResources().getDisplayMetrics().widthPixels - (((int) this.getResources().getDimension(R.dimen.d6))) * 2;
            v0 = this.r * arg4 / arg3;
        }

        return v0;
    }

    final void b() {
        if (this.w != null) {
            com.ss.android.article.base.feature.f.aa v2 = this.w;
            if (this != null) {
                View v2_1 = ((com.ss.android.article.base.feature.f.a.f) v2).h.c();
                if (v2_1 != null && v2_1.getParent() == null) {
                    this.addView(v2_1);
                }
            }

            v2 = this.w;
            String v3 = this.v;
            try {
                ((com.ss.android.article.base.feature.f.a.b) v2).d.putOpt("log_extra", v3);
            } catch (JSONException v2_2) {
            }

            this.w.a(this.r, this.s);
            this.w.a(0, this.t, this.f.itemId, null, 1, this.d);
        }

        if (this.mVideoController != null) {
            this.mVideoController.play(null, null, null, this.d, this.f, this.t, 0, this.r, this.s, null, 0, null, false, null, true, this.v, null);
        }
    }

    public final void releaseMedia() {
        if (this.w != null) {
            int v0 = this.w.n == 207 ? 1 : 0;
            if (v0 == 0) {
                this.w.b(false);
            }
        }

        if (this.mVideoController != null && this.mVideoController.isVideoVisible()) {
            this.mVideoController.releaseMedia();
        }
    }

    public final int getLayoutRes() {
        return R.layout.new_detail_ad_video;
    }

    protected final void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.c != null) {
            this.c.a(((z) this));
        }
    }

    protected final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.c != null) {
            this.c.a();
        }

        this.releaseMedia();
    }

    public final void setAdImage(Image arg1) {
    }

    public final void setArticle(Article arg1) {
        this.f = arg1;
    }
}

